#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>
struct ListNode {
    int val;
    struct ListNode* next;
    
};

typedef struct ListNode LNode;
struct ListNode* getIntersectionNode(struct ListNode* headA, struct ListNode* headB) {
    if (headA == NULL || headB == NULL)
    {
        return NULL;
    }
    LNode* pa = headA;
    LNode* pb = headB;
    int lengA = 1;
    int lengB = 1;
    while (pa->next)
    {
        pa = pa->next;
        ++lengA;
    }
    while (pb->next)
    {
        pb = pb->next;
        ++lengB;
    }
    if (pa != pb)
    {
        return NULL;
    }
    pa = headA;
    pb = headB;
    if (lengA < lengB)
    {
        int tmp = lengA;
        lengA = lengB;
        lengB = tmp;
        LNode* ptmp = pa;
        pa = pb;
        pb = ptmp;
    }
    for (int i = lengA - lengB; i > 0; --i)
    {
        pa = pa->next;
    }
    while (pa != pb)
    {
        pa = pa->next;
        pb = pb->next;
    }
    return pa;
}